// pages/index/indexdeatil/indexdeatil.js

let store=require("../../../utils/store.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail:{},
    Imageurl:"/images/周一.jpg",
    IsImage:true,
    IsAgree:false,
    agreeNumber:0,
    comment:[],
    CommentText:"",
    ideaid:0,
  },

  //点击发送
  Clickemit(){
    var that=this
    wx.request({
      url: 'http://localhost:8080/addComment',
      data:{
        openid:store.getItem("openId"),
        ideaid:that.data.detail.ideaid,
        contentdetails:that.data.CommentText
      },
      success:res=>{
        that.getComment();
      }
    })
  },

  //删除评论
  delectComment(item){
    console.log(item.currentTarget.dataset.item)
    var commentid=item.currentTarget.dataset.item.commentid;
    var openid=item.currentTarget.dataset.item.openid;
    var that=this

  if(openid==store.getItem('openId')){
    wx.showModal({
      cancelColor: 'cancelColor',
      title:"提示",
      content:"是否确认删除",
      success (res){
        wx.request({
          url: 'http://localhost:8080/delectComment',
          data:{
            commentid:commentid
          },
          success:ress=>{
            that.getComment();
          }
        })
      }
    })
    }else{
      wx.showToast({
        title: '无法删除',
        icon: 'none',
        duration: 2000
      })
    }
    
  },



  //监听输入的内容
  CommentText(e){
    var that=this
    that.setData({
      CommentText:e.detail.value
    })
  },

  //请求网络评论
  getComment(){
    var that=this
    // console.log(that.data.detail)
    // console.log(that.data.detail.ideaid)
    wx.request({
      url: 'http://localhost:8080/findCommentByIdeaid',
      data:{
        ideaid:that.data.detail.ideaid
      },
      success:res=>{
        console.log(res)
        that.setData({
          comment:res.data
        })
      }
    })
  },

  //点击小拳头
  Clickagree(){
    var that=this
    that.setData({
     IsAgree:!that.data.IsAgree
    })
    if(that.data.IsAgree){
      that.setData({
        agreeNumber:that.data.agreeNumber+1
      })
      wx.request({
        url: 'http://localhost:8080/updatatumbupnumber',
        data:{
          ideaid:that.data.detail.ideaid,
          openid:store.getItem("openId"),
          tumbupnumber:that.data.agreeNumber
        }
      })
    }else{
      that.setData({
        agreeNumber:that.data.agreeNumber-1
      })
      wx.request({
        url: 'http://localhost:8080/delectnumber',
        data:{
          ideaid:that.data.detail.ideaid,
          openid:store.getItem("openId"),
          tumbupnumber:that.data.agreeNumber
        }
      })
    }
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    // console.log(JSON.parse(options.detail))
    // that.setData({
    //   detail:JSON.parse(options.detail),
    //   IsAgree:JSON.parse(options.detail).status,
    //   agreeNumber:JSON.parse(options.detail).tumbupnumber,
    //   ideaid:JSON.parse(options.detail).ideaid,
    // })
    // if(that.data.detail.pictureroute){
    //   that.setData({
    //     Imageurl:" "+JSON.parse(options.detail).pictureroute
    //   })
    // }else{
    //   that.setData({
    //     IsImage:false
    //   })
    // }

    that.setData({
      detail:JSON.parse(decodeURIComponent(options.detail)),
      IsAgree:JSON.parse(decodeURIComponent(options.detail)).status,
      agreeNumber:JSON.parse(decodeURIComponent(options.detail)).tumbupnumber,
      ideaid:JSON.parse(decodeURIComponent(options.detail)).ideaid,
    })
    if(that.data.detail.pictureroute){
      that.setData({
        Imageurl:" "+JSON.parse(decodeURIComponent(options.detail)).pictureroute
      })
    }else{
      that.setData({
        IsImage:false
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this
    that.getComment();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})